package cn.kgc.ssm.controller;

import cn.hutool.captcha.LineCaptcha;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author: mosin
 * @version: v1.0  2022/12/16
 * 获取验证码
 */
@Controller
@RequestMapping("captcha")
public class CaptchaController {

    @RequestMapping("getCode")
    public void  getCode(HttpServletResponse response , HttpSession session){
        LineCaptcha lineCaptcha = new LineCaptcha(110, 50, 4, 3);

        String code = lineCaptcha.getCode();
        session.setAttribute("code", code);

        try {
            ServletOutputStream outputStream = response.getOutputStream();
            lineCaptcha.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
